/*
 * Ganesa Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.ganesa.application.ui.dialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import be.gnx.ganesa.application.plugin.BoundingBoxPlugin;
import be.gnx.ganesa.application.plugin.ImportPlugin;
import be.gnx.ganesa.application.ui.widget.BoundingBoxTabItemControl;
import be.gnx.ganesa.application.ui.widget.OSMSlippyMapBoundingBoxTabItemControl;
import be.gnx.ganesa.application.ui.widget.PreferencesBoundingBoxTabItemControl;
import be.gnx.ganesa.application.ui.widget.SlippyMapBoundingBoxTabItemControl;
import be.gnx.ganesa.application.ui.widget.ImportTabItemControl;

public class ImportDialog extends Dialog {

  public class ImportDialogResult {
    public BoundingBoxTabItemControl boundingBoxTabItemControl;
    public BoundingBoxPlugin boundingBoxPlugin;
    public ImportTabItemControl importTabItemControl;
    public ImportPlugin importPlugin;
  }

  private CTabFolder boundingBoxTabFolder;

  private PreferencesBoundingBoxTabItemControl preferencesBoundingBoxTabItemControl;

  private SlippyMapBoundingBoxTabItemControl slippyMapBoundingBoxTabItemControl;

  private List<BoundingBoxTabItemControl> boundingBoxTabItemControls = new ArrayList<BoundingBoxTabItemControl>(); 

  private CTabFolder importTabFolder;

  private Vector<ImportTabItemControl> importTabItemControls = new Vector<ImportTabItemControl>();

  public ImportDialog(Shell parent, int style) {
    super(parent, style);
  }

  public ImportDialog(Shell parent) {
    this(parent, 0);
  }

  public ImportDialogResult open() {
    Shell parent = getParent();
    Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    shell.setSize(parent.getClientArea().width / 2, parent.getClientArea().height / 2);
    shell.setText(getText());

    boundingBoxTabFolder = new CTabFolder(shell, SWT.TOP | SWT.FLAT | SWT.BORDER | SWT.MULTI);
    CTabItem preferencesBoundingBoxTabItem = new CTabItem(boundingBoxTabFolder, SWT.NONE);
    preferencesBoundingBoxTabItem.setText("BBox");
    preferencesBoundingBoxTabItemControl = new PreferencesBoundingBoxTabItemControl(boundingBoxTabFolder, SWT.NONE);
    preferencesBoundingBoxTabItem.setControl(preferencesBoundingBoxTabItemControl);
    boundingBoxTabItemControls.add(preferencesBoundingBoxTabItemControl);
    CTabItem slippyMapBoundingBoxTabItem = new CTabItem(boundingBoxTabFolder, SWT.NONE);
    slippyMapBoundingBoxTabItem.setText("OSM Slippy Map");
    slippyMapBoundingBoxTabItemControl = new OSMSlippyMapBoundingBoxTabItemControl(boundingBoxTabFolder, SWT.NONE);
    slippyMapBoundingBoxTabItem.setControl(slippyMapBoundingBoxTabItemControl);
    boundingBoxTabItemControls.add(slippyMapBoundingBoxTabItemControl);

    importTabFolder = new CTabFolder(shell, SWT.TOP | SWT.FLAT | SWT.BORDER | SWT.MULTI);
    for (ImportPlugin importPlugin : ImportPlugin.getImportPlugins()) {
      CTabItem importTabItem = new CTabItem(importTabFolder, SWT.NONE);
      importTabItem.setText("OSM");
      ImportTabItemControl importTabItemControl = importPlugin.newImportTabItemControl(importTabFolder, SWT.NONE);
      importTabItem.setControl(importTabItemControl);
      importTabItemControls.add(importTabItemControl);
    }

    boundingBoxTabFolder.setSelection(0);
    importTabFolder.setSelection(0);

    resize();

    shell.open();
    Display display = parent.getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }

    ImportDialogResult importDialogResult = new ImportDialogResult();
    importDialogResult.boundingBoxTabItemControl = boundingBoxTabItemControls.get(boundingBoxTabFolder.getSelectionIndex());
    importDialogResult.importTabItemControl = importTabItemControls.get(importTabFolder.getSelectionIndex());
    importDialogResult.importPlugin = ImportPlugin.getImportPlugins().get(importTabFolder.getSelectionIndex());
    return importDialogResult;
  }

  public void resize() {
    boundingBoxTabFolder.setBounds(20, 20, boundingBoxTabFolder.getParent().getClientArea().width - 40, (boundingBoxTabFolder.getParent().getClientArea().height - 60) / 2);
    importTabFolder.setBounds(20, (importTabFolder.getParent().getClientArea().height / 2) + 10, importTabFolder.getParent().getClientArea().width - 40, (importTabFolder.getParent().getClientArea().height - 60) / 2);
    //osmImportTabItem.resize();
    //osmImport.setBounds(cTabFolder.getBounds());
  }

}